<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, minimal-ui">
    <title>marked</title>
    <link type="text/css" rel="stylesheet" href="assets/css/github-markdown.css">
    <link type="text/css" rel="stylesheet" href="assets/css/pilcrow.css">
    <link type="text/css" rel="stylesheet" href="assets/css/hljs-github.min.css"/>
  </head>
  <body>
    <article class="markdown-body"><h1 id="marked">marked</h1>
<blockquote>
<p>A full-featured markdown parser and compiler, written in JavaScript. Built
for speed.</p>
</blockquote>
<p><a href="http://badge.fury.io/js/marked"><img src="https://badge.fury.io/js/marked.png" alt="NPM version"></a></p>
<h2 id="install">Install</h2>
<pre class="hljs"><code>npm install marked --save</code></pre><h2 id="usage">Usage</h2>
<p>Minimal usage:</p>
<pre class="hljs"><code><span class="hljs-keyword">var</span> marked = <span class="hljs-built_in">require</span>(<span class="hljs-string">'marked'</span>);
<span class="hljs-built_in">console</span>.log(marked(<span class="hljs-string">'I am using __markdown__.'</span>));
<span class="hljs-comment">// Outputs: &lt;p&gt;I am using &lt;strong&gt;markdown&lt;/strong&gt;.&lt;/p&gt;</span></code></pre><p>Example setting options with default values:</p>
<pre class="hljs"><code><span class="hljs-keyword">var</span> marked = <span class="hljs-built_in">require</span>(<span class="hljs-string">'marked'</span>);
marked.setOptions({
  <span class="hljs-attr">renderer</span>: <span class="hljs-keyword">new</span> marked.Renderer(),
  <span class="hljs-attr">gfm</span>: <span class="hljs-literal">true</span>,
  <span class="hljs-attr">tables</span>: <span class="hljs-literal">true</span>,
  <span class="hljs-attr">breaks</span>: <span class="hljs-literal">false</span>,
  <span class="hljs-attr">pedantic</span>: <span class="hljs-literal">false</span>,
  <span class="hljs-attr">sanitize</span>: <span class="hljs-literal">false</span>,
  <span class="hljs-attr">smartLists</span>: <span class="hljs-literal">true</span>,
  <span class="hljs-attr">smartypants</span>: <span class="hljs-literal">false</span>
});

<span class="hljs-built_in">console</span>.log(marked(<span class="hljs-string">'I am using __markdown__.'</span>));</code></pre><h3 id="browser">Browser</h3>
<pre class="hljs"><code><span class="hljs-meta">&lt;!doctype html&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"utf-8"</span>/&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Marked in the browser<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"lib/marked.js"</span>&gt;</span><span class="undefined"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"content"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">
    <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'content'</span>).innerHTML =
      marked(<span class="hljs-string">'# Marked in browser\n\nRendered by **marked**.'</span>);
  </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span></code></pre><h2 id="marked(markdownstring-[,options]-[,callback])">marked(markdownString [,options] [,callback])</h2>
<h3 id="markdownstring">markdownString</h3>
<p>Type: <code>string</code></p>
<p>String of markdown source to be compiled.</p>
<h3 id="options">options</h3>
<p>Type: <code>object</code></p>
<p>Hash of options. Can also be set using the <code>marked.setOptions</code> method as seen
above.</p>
<h3 id="callback">callback</h3>
<p>Type: <code>function</code></p>
<p>Function called when the <code>markdownString</code> has been fully parsed when using
async highlighting. If the <code>options</code> argument is omitted, this can be used as
the second argument.</p>
<h2 id="options-1">Options</h2>
<h3 id="highlight">highlight</h3>
<p>Type: <code>function</code></p>
<p>A function to highlight code blocks. The first example below uses async highlighting with
<a href="https://github.com/rvagg/node-pygmentize-bundled">node-pygmentize-bundled</a>, and the second is a synchronous example using
<a href="https://github.com/isagalaev/highlight.js">highlight.js</a>:</p>
<pre class="hljs"><code><span class="hljs-keyword">var</span> marked = <span class="hljs-built_in">require</span>(<span class="hljs-string">'marked'</span>);

<span class="hljs-keyword">var</span> markdownString = <span class="hljs-string">'```js\n console.log("hello"); \n```'</span>;

<span class="hljs-comment">// Async highlighting with pygmentize-bundled</span>
marked.setOptions({
  <span class="hljs-attr">highlight</span>: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">code, lang, callback</span>) </span>{
    <span class="hljs-built_in">require</span>(<span class="hljs-string">'pygmentize-bundled'</span>)({ <span class="hljs-attr">lang</span>: lang, <span class="hljs-attr">format</span>: <span class="hljs-string">'html'</span> }, code, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, result</span>) </span>{
      callback(err, result.toString());
    });
  }
});

<span class="hljs-comment">// Using async version of marked</span>
marked(markdownString, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, content</span>) </span>{
  <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
  <span class="hljs-built_in">console</span>.log(content);
});

<span class="hljs-comment">// Synchronous highlighting with highlight.js</span>
marked.setOptions({
  <span class="hljs-attr">highlight</span>: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">code</span>) </span>{
    <span class="hljs-keyword">return</span> <span class="hljs-built_in">require</span>(<span class="hljs-string">'highlight.js'</span>).highlightAuto(code).value;
  }
});

<span class="hljs-built_in">console</span>.log(marked(markdownString));</code></pre><h4 id="highlight-arguments">highlight arguments</h4>
<p><code>code</code></p>
<p>Type: <code>string</code></p>
<p>The section of code to pass to the highlighter.</p>
<p><code>lang</code></p>
<p>Type: <code>string</code></p>
<p>The programming language specified in the code block.</p>
<p><code>callback</code></p>
<p>Type: <code>function</code></p>
<p>The callback function to call when using an async highlighter.</p>
<h3 id="renderer">renderer</h3>
<p>Type: <code>object</code>
Default: <code>new Renderer()</code></p>
<p>An object containing functions to render tokens to HTML.</p>
<h4 id="overriding-renderer-methods">Overriding renderer methods</h4>
<p>The renderer option allows you to render tokens in a custom manner. Here is an
example of overriding the default heading token rendering by adding an embedded anchor tag like on GitHub:</p>
<pre class="hljs"><code><span class="hljs-keyword">var</span> marked = <span class="hljs-built_in">require</span>(<span class="hljs-string">'marked'</span>);
<span class="hljs-keyword">var</span> renderer = <span class="hljs-keyword">new</span> marked.Renderer();

renderer.heading = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">text, level</span>) </span>{
  <span class="hljs-keyword">var</span> escapedText = text.toLowerCase().replace(<span class="hljs-regexp">/[^\w]+/g</span>, <span class="hljs-string">'-'</span>);

  <span class="hljs-keyword">return</span> <span class="hljs-string">'&lt;h'</span> + level + <span class="hljs-string">'&gt;&lt;a name="'</span> +
                escapedText +
                 <span class="hljs-string">'" class="anchor" href="#'</span> +
                 escapedText +
                 <span class="hljs-string">'"&gt;&lt;span class="header-link"&gt;&lt;/span&gt;&lt;/a&gt;'</span> +
                  text + <span class="hljs-string">'&lt;/h'</span> + level + <span class="hljs-string">'&gt;'</span>;
},

<span class="hljs-built_in">console</span>.log(marked(<span class="hljs-string">'# heading+'</span>, { <span class="hljs-attr">renderer</span>: renderer }));</code></pre><p>This code will output the following HTML:</p>
<pre class="hljs"><code><span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"heading-"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"anchor"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#heading-"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"header-link"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
  heading+
<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span></code></pre><h4 id="block-level-renderer-methods">Block level renderer methods</h4>
<ul class="list">
<li>code(<em>string</em> code, <em>string</em> language)</li>
<li>blockquote(<em>string</em> quote)</li>
<li>html(<em>string</em> html)</li>
<li>heading(<em>string</em> text, <em>number</em>  level)</li>
<li>hr()</li>
<li>list(<em>string</em> body, <em>boolean</em> ordered)</li>
<li>listitem(<em>string</em>  text)</li>
<li>paragraph(<em>string</em> text)</li>
<li>table(<em>string</em> header, <em>string</em> body)</li>
<li>tablerow(<em>string</em> content)</li>
<li>tablecell(<em>string</em> content, <em>object</em> flags)</li>
</ul>
<p><code>flags</code> has the following properties:</p>
<pre class="hljs"><code>{
    <span class="hljs-attr">header</span>: <span class="hljs-literal">true</span> || <span class="hljs-literal">false</span>,
    <span class="hljs-attr">align</span>: <span class="hljs-string">'center'</span> || <span class="hljs-string">'left'</span> || <span class="hljs-string">'right'</span>
}</code></pre><h4 id="inline-level-renderer-methods">Inline level renderer methods</h4>
<ul class="list">
<li>strong(<em>string</em> text)</li>
<li>em(<em>string</em> text)</li>
<li>codespan(<em>string</em> code)</li>
<li>br()</li>
<li>del(<em>string</em> text)</li>
<li>link(<em>string</em> href, <em>string</em> title, <em>string</em> text)</li>
<li>image(<em>string</em> href, <em>string</em> title, <em>string</em> text)</li>
</ul>
<h3 id="gfm">gfm</h3>
<p>Type: <code>boolean</code>
Default: <code>true</code></p>
<p>Enable <a href="https://help.github.com/articles/github-flavored-markdown">GitHub flavored markdown</a>.</p>
<h3 id="tables">tables</h3>
<p>Type: <code>boolean</code>
Default: <code>true</code></p>
<p>Enable GFM <a href="https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet#wiki-tables">tables</a>.
This option requires the <code>gfm</code> option to be true.</p>
<h3 id="breaks">breaks</h3>
<p>Type: <code>boolean</code>
Default: <code>false</code></p>
<p>Enable GFM <a href="https://help.github.com/articles/github-flavored-markdown#newlines">line breaks</a>.
This option requires the <code>gfm</code> option to be true.</p>
<h3 id="pedantic">pedantic</h3>
<p>Type: <code>boolean</code>
Default: <code>false</code></p>
<p>Conform to obscure parts of <code>markdown.pl</code> as much as possible. Don&#39;t fix any of
the original markdown bugs or poor behavior.</p>
<h3 id="sanitize">sanitize</h3>
<p>Type: <code>boolean</code>
Default: <code>false</code></p>
<p>Sanitize the output. Ignore any HTML that has been input.</p>
<h3 id="smartlists">smartLists</h3>
<p>Type: <code>boolean</code>
Default: <code>true</code></p>
<p>Use smarter list behavior than the original markdown. May eventually be
default with the old behavior moved into <code>pedantic</code>.</p>
<h3 id="smartypants">smartypants</h3>
<p>Type: <code>boolean</code>
Default: <code>false</code></p>
<p>Use &quot;smart&quot; typograhic punctuation for things like quotes and dashes.</p>
<h2 id="access-to-lexer-and-parser">Access to lexer and parser</h2>
<p>You also have direct access to the lexer and parser if you so desire.</p>
<pre class="hljs"><code><span class="hljs-keyword">var</span> tokens = marked.lexer(text, options);
<span class="hljs-built_in">console</span>.log(marked.parser(tokens));</code></pre><pre class="hljs"><code><span class="hljs-keyword">var</span> lexer = <span class="hljs-keyword">new</span> marked.Lexer(options);
<span class="hljs-keyword">var</span> tokens = lexer.lex(text);
<span class="hljs-built_in">console</span>.log(tokens);
<span class="hljs-built_in">console</span>.log(lexer.rules);</code></pre><h2 id="cli">CLI</h2>
<pre class="hljs"><code>$ marked -o hello.html
hello world
^D
$ cat hello.html
&lt;p&gt;hello world&lt;/p&gt;</code></pre><h2 id="philosophy-behind-marked">Philosophy behind marked</h2>
<p>The point of marked was to create a markdown compiler where it was possible to
frequently parse huge chunks of markdown without having to worry about
caching the compiled output somehow...or blocking for an unnecesarily long time.</p>
<p>marked is very concise and still implements all markdown features. It is also
now fully compatible with the client-side.</p>
<p>marked more or less passes the official markdown test suite in its
entirety. This is important because a surprising number of markdown compilers
cannot pass more than a few tests. It was very difficult to get marked as
compliant as it is. It could have cut corners in several areas for the sake
of performance, but did not in order to be exactly what you expect in terms
of a markdown rendering. In fact, this is why marked could be considered at a
disadvantage in the benchmarks above.</p>
<p>Along with implementing every markdown feature, marked also implements <a href="http://github.github.com/github-flavored-markdown/">GFM
features</a>.</p>
<h2 id="benchmarks">Benchmarks</h2>
<p>node v0.8.x</p>
<pre class="hljs"><code>$ node <span class="hljs-built_in">test</span> --bench
marked completed <span class="hljs-keyword">in</span> 3411ms.
marked (gfm) completed <span class="hljs-keyword">in</span> 3727ms.
marked (pedantic) completed <span class="hljs-keyword">in</span> 3201ms.
robotskirt completed <span class="hljs-keyword">in</span> 808ms.
showdown (reuse converter) completed <span class="hljs-keyword">in</span> 11954ms.
showdown (new converter) completed <span class="hljs-keyword">in</span> 17774ms.
markdown-js completed <span class="hljs-keyword">in</span> 17191ms.</code></pre><p><strong>Marked is now faster than Discount, which is written in C.</strong></p>
<p>For those feeling skeptical: These benchmarks run the entire markdown test suite 1000 times. The test suite tests every feature. It doesn&#39;t cater to specific aspects.</p>
<h3 id="pro-level">Pro level</h3>
<p>You also have direct access to the lexer and parser if you so desire.</p>
<pre class="hljs"><code><span class="hljs-keyword">var</span> tokens = marked.lexer(text, options);
<span class="hljs-built_in">console</span>.log(marked.parser(tokens));</code></pre><pre class="hljs"><code><span class="hljs-keyword">var</span> lexer = <span class="hljs-keyword">new</span> marked.Lexer(options);
<span class="hljs-keyword">var</span> tokens = lexer.lex(text);
<span class="hljs-built_in">console</span>.log(tokens);
<span class="hljs-built_in">console</span>.log(lexer.rules);</code></pre><pre class="hljs"><code>$ node
&gt; require(<span class="hljs-string">'marked'</span>).lexer(<span class="hljs-string">'&gt; i am using marked.'</span>)
[ { <span class="hljs-built_in">type</span>: <span class="hljs-string">'blockquote_start'</span> },
  { <span class="hljs-built_in">type</span>: <span class="hljs-string">'paragraph'</span>,
    text: <span class="hljs-string">'i am using marked.'</span> },
  { <span class="hljs-built_in">type</span>: <span class="hljs-string">'blockquote_end'</span> },
  links: {} ]</code></pre><h2 id="running-tests-&-contributing">Running Tests &amp; Contributing</h2>
<p>If you want to submit a pull request, make sure your changes pass the test
suite. If you&#39;re adding a new feature, be sure to add your own test.</p>
<p>The marked test suite is set up slightly strangely: <code>test/new</code> is for all tests
that are not part of the original markdown.pl test suite (this is where your
test should go if you make one). <code>test/original</code> is only for the original
markdown.pl tests. <code>test/tests</code> houses both types of tests after they have been
combined and moved/generated by running <code>node test --fix</code> or <code>marked --test
--fix</code>.</p>
<p>In other words, if you have a test to add, add it to <code>test/new/</code> and then
regenerate the tests with <code>node test --fix</code>. Commit the result. If your test
uses a certain feature, for example, maybe it assumes GFM is <em>not</em> enabled, you
can add <code>.nogfm</code> to the filename. So, <code>my-test.text</code> becomes
<code>my-test.nogfm.text</code>. You can do this with any marked option. Say you want
line breaks and smartypants enabled, your filename should be:
<code>my-test.breaks.smartypants.text</code>.</p>
<p>To run the tests:</p>
<pre class="hljs"><code><span class="hljs-built_in">cd</span> marked/
node <span class="hljs-built_in">test</span></code></pre><h3 id="contribution-and-license-agreement">Contribution and License Agreement</h3>
<p>If you contribute code to this project, you are implicitly allowing your code
to be distributed under the MIT license. You are also implicitly verifying that
all code is your original work. <code>&lt;/legalese&gt;</code></p>
<h2 id="license">License</h2>
<p>Copyright (c) 2011-2014, Christopher Jeffrey. (MIT License)</p>
<p>See LICENSE for more info.</p>
    </article>
  </body>
</html>
